//
//  LuckyUtils.m
//  SMTools
//
//  Created by fengsh on 2018/12/29.
//  Copyright © 2018年 fengsh. All rights reserved.
//

#import "LuckyUtils.h"

@implementation LuckyUtils

+ (void)lucky_show:(NSString *)msg
{
    NSAlert *alert = [[NSAlert alloc] init];
    alert.messageText = @"提示";
    [alert setShowsHelp:NO];
    alert.informativeText = msg;
    alert.alertStyle = NSAlertStyleWarning;
    [alert addButtonWithTitle:@"确定"];
    [alert runModal];
}

+ (void)lucky_showAlert:(NSString *)title info:(NSString *)msg inWindow:(NSWindow *)wnd completed:(void(^)(NSInteger result))finish
{
    NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:@"确定"];
    [alert addButtonWithTitle:@"取消"];
    [alert setMessageText:title];
    [alert setInformativeText:msg];
    [alert setAlertStyle:NSWarningAlertStyle];
    [alert beginSheetModalForWindow:wnd completionHandler:finish];
}

+ (NSString *)lucky_dictionaryvalue:(id)obj
{
    if ([obj isKindOfClass:[NSNull class]]) {
        return nil;
    }
    
    return obj;
}

+ (NSImage*) resizeImage:(NSImage*)sourceImage size:(NSSize)size
{
    NSRect targetFrame = NSMakeRect(0, 0, size.width, size.height);
    NSImage*  targetImage = [[NSImage alloc] initWithSize:size];
    
    NSSize sourceSize = [sourceImage size];
    
    float ratioH = size.height/ sourceSize.height;
    float ratioW = size.width / sourceSize.width;
    
    NSRect cropRect = NSZeroRect;
    if (ratioH >= ratioW) {
        cropRect.size.width = floor (size.width / ratioH);
        cropRect.size.height = sourceSize.height;
    } else {
        cropRect.size.width = sourceSize.width;
        cropRect.size.height = floor(size.height / ratioW);
    }
    
    cropRect.origin.x = floor( (sourceSize.width - cropRect.size.width)/2 );
    cropRect.origin.y = floor( (sourceSize.height - cropRect.size.height)/2 );
    
    
    
    [targetImage lockFocus];
    
    [sourceImage drawInRect:targetFrame
                   fromRect:cropRect       //portion of source image to draw
                  operation:NSCompositeCopy  //compositing operation
                   fraction:1.0              //alpha (transparency) value
             respectFlipped:YES              //coordinate system
                      hints:@{NSImageHintInterpolation:
                                  [NSNumber numberWithInt:NSImageInterpolationLow]}];
    
    [targetImage unlockFocus];
    return targetImage;
}

+ (NSString *)uniqueid
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyMMddHHmmss"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    int randomValue =arc4random() %[ dateString length];
    NSString *unique = [NSString stringWithFormat:@"%@.%d",dateString,randomValue];
    return unique;
}

+ (BOOL)isNumber:(NSString *)string
{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}

+ (NSString *)uuid16
{
    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
    CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
    NSString *uuid = [NSString stringWithString:(__bridge NSString *)uuid_string_ref];
    uuid = [uuid stringByReplacingOccurrencesOfString:@"-" withString:@""];
    CFRelease(uuid_ref);
    CFRelease(uuid_string_ref);
    return [[uuid substringToIndex:16] lowercaseString];
}

@end
